#include "Command.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

// 用于打印分隔线
void PrintSeparator(const std::string &testName)
{
    std::cout << "\n=== " << testName << " ===" << std::endl;
}

// 测试用的普通函数
int Add(int a, int b)
{
    return a + b;
}

// 测试用的类
class Calculator
{
public:
    int Multiply(int a, int b) const
    {
        return a * b;
    }

    std::string GetOperation() const
    {
        return "Calculator";
    }
};

int main()
{
    // 测试1：包装普通函数
    PrintSeparator("测试普通函数包装");
    {
        CommCommand<int> cmd;
        cmd.Wrap(Add, 2, 3);
        std::cout << "Add(2, 3) = " << cmd.Excecute() << std::endl;
        // 预期输出: 5
    }

    // 测试2：包装 lambda 表达式
    PrintSeparator("测试Lambda表达式");
    {
        CommCommand<int> cmd;
        auto lambda = [](int x)
        { return x * x; };
        cmd.Wrap(lambda, 4);
        std::cout << "lambda(4) = " << cmd.Excecute() << std::endl;
        // 预期输出: 16
    }

    // 测试3：包装类的const成员函数
    PrintSeparator("测试类的const成员函数");
    {
        CommCommand<int> cmd;
        Calculator calc;
        cmd.Wrap(&Calculator::Multiply, &calc, 3, 4);
        std::cout << "Calculator::Multiply(3, 4) = " << cmd.Excecute() << std::endl;
        // 预期输出: 12
    }

    // 测试4：包装返回string的成员函数
    PrintSeparator("测试返回string的成员函数");
    {
        CommCommand<std::string> cmd;
        Calculator calc;
        cmd.Wrap(&Calculator::GetOperation, &calc);
        std::cout << "Calculator::GetOperation() = " << cmd.Excecute() << std::endl;
        // 预期输出: Calculator
    }

    // 测试5：void返回类型
    PrintSeparator("测试void返回类型");
    {
        CommCommand<void> cmd;
        bool called = false;
        cmd.Wrap([&called]()
                 { 
            called = true;
            std::cout << "Void lambda executed" << std::endl; });
        cmd.Excecute();
        std::cout << "Lambda was called: " << std::boolalpha << called << std::endl;
    }

    // 测试6：命令队列
    PrintSeparator("测试命令队列");
    {
        std::vector<CommCommand<int>> cmdQueue;
        Calculator calc;

        // 创建多个命令
        CommCommand<int> cmd1, cmd2, cmd3;
        cmd1.Wrap(Add, 1, 2);
        cmd2.Wrap(&Calculator::Multiply, &calc, 3, 4);
        cmd3.Wrap([](int x)
                  { return x * x; }, 5);

        cmdQueue.push_back(cmd1);
        cmdQueue.push_back(cmd2);
        cmdQueue.push_back(cmd3);

        // 执行命令队列
        std::cout << "执行命令队列结果：" << std::endl;
        for (size_t i = 0; i < cmdQueue.size(); ++i)
        {
            std::cout << "Command " << (i + 1) << " result: "
                      << cmdQueue[i].Excecute() << std::endl;
        }
        // 预期输出:
        // Command 1 result: 3 (1 + 2)
        // Command 2 result: 12 (3 * 4)
        // Command 3 result: 25 (5 * 5)
    }

    return 0;
}